package com.atguigu.controller;

import com.atguigu.entity.system.SysRole;
import com.atguigu.service.SysRoleService;
import com.atguigu.vo.common.Result;
import com.atguigu.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;


@Tag(name = "权限管理")
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Operation(summary = "分页")
    @GetMapping("/page/{current}/{limit}")
    public Result page(@PathVariable("current") Integer current, @PathVariable("limit") Integer limit,
                       @RequestParam(required = false) String roleName) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(current, limit, roleName);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增")
    @PostMapping("/submit")
    public Result submit(@RequestBody SysRole sysRole) {
        sysRoleService.submit(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        sysRoleService.update(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result remove(@RequestParam(name = "id") long roleId) {
        sysRoleService.remove(roleId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取所有角色")
    @GetMapping("/findAll/{userId}")
    public Result findAll(@PathVariable Long userId) {
        Map<String,Object> roleList = sysRoleService.findAll(userId);
        return Result.build(roleList, ResultCodeEnum.SUCCESS);
    }

}
